// 将工站根据不同类型分类
function getWorkStationArr (){
    workStationArr = []
    let work = '',
    	action = '' ,
    	arr = [], // 工站信息
    	data = getWorkstation(),
    	locationList = getLocations(),
    	arr1 = [],
        nameArr=[],
		typeArr=[]
	locationList.map(item =>{
		obj=item.properties.type
		let _index  = typeArr.some(number => number.value==obj);
		if (!_index){
			typeArr.push({
				value:obj,
				text:obj,
				children:[]
			})
		}
	})
	locationList.map(e=>{
		let children =[]
		typeArr.map(item =>{
			if(item.value === e.properties.type){
				item.children.push({
					value:e.name,
					text:e.name,
					type:e.type.name
				})
			}
		})
	}) 
    data.map(item => {
    	var index=item.name.indexOf("\_");
    	let obj=''
    	obj=item.name.substring(0,index)

    	let _index  = nameArr.some(number => number.value==obj); 
    	if (!_index){
    		nameArr.push({
    			value:obj,
    			text:obj,
    			children:[]
    		})
    	}

    	// arr.push({
    	// 	value: item.siteName,
    	// 	text: item.name,
    	// })
    }) 
    data.map(item=>{
    	var index=item.name.indexOf("\_");
    	let obj=''
		workStationTypeAll =[]
    	obj=item.name.substring(0,index) 
    	nameArr.map((e,i)=>{ 
    		if (e.value == obj){
			
    			e.children.push({
    					value: item.siteName,
    					text: item.name,
    			})
    		}
    	})
    }) 
    locationList.map(item => {
    	arr1.push({
    		value: item.type.name,
    		text: item.name
    	})
    })
    typeArr.map(e=>{
    	e.children.map(item => {
    		let list = [],
    				children = []
    		arr1.map(_e => {
    			if (item.value === _e.text) {
    				item.type = _e.value 
					 
    			}
    		})
			// if (!item.type){
			// }
			// console.log( item.type ,item,5);
			
    		list = item.type ? getLocationType(item.type) : [];
    		list.map(e => {
    			children.push({
    				value: e.value,
    				text: e.text
    			})
    		})
    		item.children = children
    	})
    }) 
	workStationTypeAll = typeArr
}

function getLocationType(type){
	let list =[]
	if (locationTypeArr && locationTypeArr.length){
		locationTypeArr.map(item=>{
			if (item.type==type){
				list =item.list
			}
		})
	}
	return list
}

//获取当前小车位置
function getvehiclesSeit() {
	let vehiclesData = [],
		flag = 0
	vehicles = ''
	$.ajax({
		url: `http://${ipValue}:55200/v1/vehiclesinfo`,
		contentType: "multipart/form-data",
		data: '',
		type: "get",
		async: !workInfo,
		dataType: "json",
		success: (res) => {
			if (res.code == 200) {
				res.msg.map(item => {
					let data = {}
					if (item.precisePosition) {
						data = {
							materialRackSerialNumber: item
								.materialRackSerialNumber,
							x: item.precisePosition.x,
							y: item.precisePosition.y,
							orientationAngle: item.orientationAngle,
							allowedOrderTypes:item.allowedOrderTypes

						}
					} else if (item.currentPosition) {
						pointsList.map(e => {
							if (e.name == item.currentPosition.name) {
								data = {
									x: e.x,
									y: e.y,
									orientationAngle: item
										.orientationAngle,
									materialRackSerialNumber: item
										.materialRackSerialNumber,
									allowedOrderTypes:item.allowedOrderTypes

								}
							}
						})
					} else {
						data = {
							allowedOrderTypes:item.allowedOrderTypes,
							materialRackSerialNumber: item
								.materialRackSerialNumber,
							x: 0,
							y: 0
						}
					}
					data.name = item.name
					if (workstation) {

						if (workstation === item.allowedOrderTypes[0]) {
							vehicles = item.name
							flag++
						}
					}

					$('.vehicles').text(vehicles)
					let allowedOrderTypes = data.allowedOrderTypes[0]
					vehiclesData.push(data)
					// if (workStationType == 'all'){
					// 	vehiclesData.push(data)
					// }else {
					// 	if (workStationType ==allowedOrderTypes){
					// 		vehiclesData.push(data)
					// 	}
					// }

				})
				vehicleSite(vehiclesData)
			}else{
				backErr(res.msg)
			}

		},
		timeout: timeOut,
		error: () => {
			// overTime()
		}
	})
}


//获取订单路线
function setLine(val) {
	let arr = [],
		flag = true,
		lineArr = [],
		siteLast = {} 
	if (val&&val.length){
		val.map(item=>{
			if (typeof item === 'string' || typeof item === "number"){ 
				$.ajax({
					url: `http://${ipValue}:55200/v1/driverorder/${item}`,
					contentType: "multipart/form-data",
					data: '',
					type: "get",
					dataType: "json",
					success: (res) => {
						if (res.code == 200) {
							let steps = res.msg.route.steps
							arr = res.msg.route.steps.reverse()
							if (res.msg.destination.operation) {
								$('#orderInfo').html(`
									<span> 动作：${getChinese(res.msg.destination.operation)} </span>
								`)
							} else {

							}
							siteLast = {
								y: steps[0].destinationPoint.position.y,
								x: steps[0].destinationPoint.position.x,
								name: steps[0].destinationPoint.name,
							} 
							if (arr && arr.length) {
								arr.map((e, index) => {
									if (e.destinationPoint.name == currentPositionId) {
										flag = false
									}
									if (flag) {
										lineArr.push({
											y: e.destinationPoint.position.y,
											x: e.destinationPoint.position.x,
											name: e.destinationPoint.name,
											y1: e.sourcePoint.position.y,
											x1: e.sourcePoint.position.x,
										})
									}

								})

							}
							driving(lineArr.reverse()) 
							drawSiteLast(siteLast)
						} else {
							backErr(res.msg)
						}
					},
					timeout: timeOut,
					error: () => {
						// overTime()
					}
				})
			}
		})
	}

	if (orderNum) {

	} else {

		$('#orderInfo').html(`
       			<span> 动作：</span>
       		`)
		driving([])
		drawSiteLast({})
	}

}
// 订单
function getAllOrder(){
	let arr = []
	$.ajax({
		url: `http://${ipValue}:55200/v1/transportOrders`,
		contentType: "multipart/form-data",
		data: '',
		type: "get",
        async: !workInfo,
		dataType: "json",
		success: (res) => { 
			if (res.code == 200) {
				arr = res.msg
			} else {
				backErr(res.msg)
			}
		},
		timeout: timeOut,
		error: () => {
			// overTime()
		}
	})
	return arr
}